<?php

session_start();
require_once('connect.php');
require_once('includes/user.class.php');
require_once('includes/classes.class.php');
require_once('includes/subjects.class.php');
require_once('includes/teachers.class.php');
require_once('includes/rooms.class.php');

$database = Database::getInstance();
$user = new User();
$classes = new Classes();
$subjects = new Subject();
$teachers = new Teachers();
$rooms = new Rooms();

if(!$user->is_logged()) header('Location: index.php');
require_once('partials/header.php'); //wczytaj header i menu
?>


    <div class="container">

        <div class="row">

            <div class="col-md-3">

                <?php require_once('partials/content-left-menu.php'); ?>

            </div>

            <div class="col-md-9">

                <h2>Zapisy na wybrane przedmioty</h2>

                <?php

                    if($user->is_leader()){
                            if(isset($_GET['activate'])){
                                if(is_numeric($_GET['activate'])){
                                    $id = $_GET['activate'];
                                    $date = date('Y-m-d H:i:s');
                                    $activate = $database->dbh->prepare("
                                    UPDATE classes SET active = '1', sign_start = '$date' WHERE id = '$id' LIMIT 1
                                ");

                                    if($activate->execute()) $_SESSION['alert'] = array('Pomyślnie udało się <b>aktywować</b> zapisy!', 'success');
                                    else $_SESSION['alert'] = array('Nie udało się <b>aktywować</b> zapisów!', 'danger');
                                    header('Location: classes.php');
                                }
                                else header('Location: classes.php');
                            }
                            if(isset($_GET['deactivate'])){
                                if(is_numeric($_GET['deactivate'])){
                                    $id = $_GET['deactivate'];
                                    $date = date('Y-m-d H:i:s');
                                    $deactivate = $database->dbh->prepare("
                                    UPDATE classes SET active = '0', sign_start = '$date' WHERE id = '$id' LIMIT 1
                                ");
                                    if($deactivate->execute()) $_SESSION['alert'] = array('Pomyślnie udało się <b>wyłączyć</b> zapisy!', 'success');
                                    else $_SESSION['alert'] = array('Nie udało się <b>wyłączyć</b> zapisów!', 'danger');
                                    header('Location: classes.php');
                                }
                                else header('Location: classes.php');
                            }
                    }

                    $any_enabled = '0';
                    $classesData = $classes -> get_classes();
                    foreach($classesData as $class){

                        if($class['active'] == '1') {
                            $any_enabled = '1';
                            break;
                        }
                    }

                $alert = $_SESSION['alert'];
                $user->show_alert($alert);
                unset($_SESSION['alert']);

                if($any_enabled == '1' || $user->is_leader())
                {
                    if($user->is_leader()) echo 'Uwaga! Czas trwania zapisów jest odliczany <strong>od chwili ich aktywacji</strong>!';
                ?>

				<div class="subject-table">
					<div class="subject-table-header subject-table-row">
						<div class="subject-table-header-cell">Nazwa</div>
                        <div class="subject-table-header-cell">Dzień</div>
                        <div class="subject-table-header-cell">Godzina</div>
                        <div class="subject-table-header-cell">Prowadzący</div>
                        <div class="subject-table-header-cell">Budynek</div>
                        <div class="subject-table-header-cell">Miejsca</div>
                        <?php
                         if($user->is_leader()) echo '<div class="subject-table-header-cell">Stan</div>';
                        ?>
					</div>

                    <?php

                    foreach($classesData as $class){

                        $cid = $class['id'];
                        $teacher = $teachers->get_single_teacher($class['teacher_id']);
                        $single_subject = $subjects->get_single_subject($class['subject_id']);
                        $single_room = $rooms->get_single_room($class['room_id']);

                        $count_query = $database->dbh->prepare("
                            SELECT COUNT(id)
                            FROM classes_record
                            WHERE classes_id = '$cid'
                        ");
                        if($count_query->execute()) $count = $count_query->fetchColumn();
                        else $count = 0;

                        if($class['active']=='1') {
                            $activate = '<a href="classes.php?deactivate='.$cid.'">Wyłącz</a>';
                            $additional_class = '';
                        }
                        else {
                            $activate = '<a href="classes.php?activate='.$cid.'">Włącz</a>';
                            $additional_class = ' danger-row';
                        }

                        if(strlen($additional_class)==0 || $user->is_leader()){
                            print '<div class="subject-table-row'.$additional_class.'">';
                            print'<div class="subject-table-header-cell">'.$single_subject[0]['name'].'</div>';
                            print'<div class="subject-table-header-cell">'.$single_subject[0]['date'].'</div>';
                            print'<div class="subject-table-header-cell">'.$single_subject[0]['time'].'</div>';
                            print'<div class="subject-table-header-cell">'.$teacher[0]['lastName'].'</div>';
                            print'<div class="subject-table-header-cell">'.$single_room[0]['building'].'/'.$single_room[0]['number'].'</div>';
                            print'<div class="subject-table-header-cell">'.$count.'/'.$single_room[0]['capacity'].'</div>';
                            if($user->is_leader()) print'<div class="subject-table-header-cell">'.$activate.'</div>';
                            print '</div>';
                        }

                    }
                    ?>
					
				</div>
                <?php
                } else{ //any_enabled
                    echo '<h4>Brak aktywnych zapisów.</h4>';
                }
                if($user->is_leader()){
                    if(isset($_POST['save_classes'])){
                        $required = array('subject', 'teacher', 'rooms', 'day', 'time', 'active_time');
                        $error = false;

                        foreach($required as $field) {
                            if (strlen($_POST[$field])<1) $error = true;
                        }
                        if (!$error){

                            $is_active = '0';
                            $subject = $_POST['subject'];
                            $teacher = $_POST['teacher'];
                            $rid = $_POST['rooms'];
                            $day = $_POST['day'];
                            $time = $_POST['time'];
                            $active_time = (int)$_POST['active_time'];
                            $sign_start = date('Y-m-d H:i:s');
                            if(isset($_POST['make_active'])) $is_active = '1';

                            if($classes->check_classes($subject,$teacher,$rid)){
                                $add = $classes->add_class($subject, $teacher, $rid, $sign_start, $active_time, $is_active);
                                if($add > 0){
                                    header( 'Location:classes.php');
                                }else{
                                    $alert = array('Wystąpił błąd, nie dodano zapisu.', 'danger');
                                }
                            }else{
                                $alert = array('Taki zapis już istnieje!', 'danger');
                            }

                        }else{
                        $alert = array('Nie wypełniono wszystkich wymaganych pól!', 'danger');
                        }

                    $user->show_alert($alert);
                    }
                    ?>
                        <div class="add-news-subject-area">

                            <h4>Dodaj zapis na zajęcia:</h4>

                            <?php
                                $subjectData = $subjects -> get_subjects();

                                foreach($subjectData as $id => $single_subject){
                                    $sub_date[$id] = $single_subject['date'];
                                    $time = explode(':', $single_subject['time']);
                                    $sub_time[$id] = $time[0].':'.$time[1];

                                    $sub_space[$id] = $single_subject['space'];
                                }
                                //wytnij powtarzające się dane:
                                $sub_date = array_unique($sub_date);
                                $sub_time = array_unique($sub_time);
                            ?>
                            <form action = "classes.php" method = "post">

                                <select name="subject" id="subject" class="subject-input">
                                    <option value="" disabled selected>Przedmiot</option>
                                    <?php
                                    foreach($subjectData as $id => $single_subject){
                                        echo '<option value="'.$single_subject['id'].'">'.$single_subject['name'].'</option>';
                                    }
                                    ?>
                                </select>

                                <select name="teacher" id="teacher" class="subject-input">
                                    <option value="" disabled selected>Prowadzący</option>
                                    <?php
                                    $teachersData = $teachers -> get_teachers();
                                    foreach($teachersData as $teacher) {
                                        echo '<option value="'.$teacher['id'].'">'.$teacher['lastName'].'</option>';
                                    }
                                    ?>
                                </select>

                                <select name="rooms" id="rooms" class="subject-input">
                                    <option value="" disabled selected>Budynek/Sala</option>
                                    <?php
                                    $roomsData = $rooms -> get_rooms();
                                    foreach($roomsData as $room) {
                                        $echo_room = $room['building'].'/'.$room['number'];
                                        $rid = $rooms -> get_room_id($room['building'], $room['number']);
                                        echo '<option value="'.$rid.'">'.$echo_room.'</option>';
                                    }
                                    ?>
                                </select>

                                <select name="day" id="day" class="subject-input">
                                    <option value="" disabled selected>Dzień</option>
                                    <?php
                                    foreach($sub_date as $date) echo '<option value="'.$date.'">'.$date.'</option>';
                                    ?>
                                </select>

                                <select name="time" id="time" class="subject-input">
                                    <option value="" disabled selected>Godzina</option>
                                    <?php
                                    foreach($sub_time as $time) echo '<option value="'.$time.'">'.$time.'</option>';
                                    ?>
                                </select>
                                <br/>
                                <input type="text" name="active_time" id="active_time" class="subject-input" placeholder="Czas trwania (godz)">
                                <input type="checkbox" name="make_active" value="1"> Czy aktywny?
                                <br/>
                                <input type="submit" name="save_classes" value="Stwórz zapis" class="subject-button">
                            </form>
                        </div>
                        <?php
                    }
                ?>

					<!--TYLKO DLA ADMINOW -->
            </div>

        </div>

    </div>

 
<?php
require_once('partials/footer.php');
?>
